package org.tity.test.java.sql;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.tity.sql.sql.builder.SE;
import org.tity.sql.sql.builder.SQL;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import com.google.common.base.Function;

public class DP {
	public static void main(String[] args) {
		 
		
		Function<Date,String> function = new Function<Date, String>() {
			@Override
			public String apply( Date input) {
				return new SimpleDateFormat("dd/mm/yyyy").format(input); 
			}
	};
	
	String s=function.apply(new Date());
		
		
        // String sql = "update t set name = 'x' where id < 100 limit 10";
        // String sql = "SELECT ID, NAME, AGE FROM USER WHERE ID = ? limit 2";
        // String sql = "select * from tablename limit 10";
 
	
        String sql = "select user from emp_table;update d set a=0 where id=1;update sss set kk=9 where id=0";
        sql="insert into ttt(id,f1,f2,f3,f4) values(1,?,'3a',?,now()),(3,?,'9b',?,sysdate)";
        sql="update ttt set f1=1,f2=4 where id=9";
    	SQL sqlobj=new SE(sql,1,2,3,4);
    	sql=sqlobj.getNameParameterSQL();
        String dbType = JdbcConstants.MYSQL;
 
        //格式化输出
        String result = SQLUtils.format(sql, dbType);
       
        System.out.println(result); // 缺省大写格式
        List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
 
        //解析出的独立语句的个数
        System.out.println("size is:" + stmtList.size());
        for (int i = 0; i < stmtList.size(); i++) {
        	 
            SQLStatement stmt = stmtList.get(i);
            System.out.println(stmt.toString());
//            SQLStatement stmt2=stmt.clone();
            MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
            stmt.accept(visitor);
 
            //获取表名称
            System.out.println("Tables : " + visitor.getTables());
            //获取操作方法名称,依赖于表名称
            System.out.println("Manipulation : " + visitor.getTables());
            //获取字段名称
            System.out.println("fields : " + visitor.getColumns());
            
            if(stmt instanceof SQLInsertStatement)
            {
            	testInsert((SQLInsertStatement)stmt,sqlobj);
            }
            if(stmt instanceof SQLUpdateStatement)
            {
            	testUpdate((SQLUpdateStatement)stmt,sqlobj);
            }
            
        }
 
    }

	private static void testUpdate(SQLUpdateStatement up, SQL sqlobj) {
		 String table=up.getTableName().getSimpleName();
		 List<SQLUpdateSetItem> items= up.getItems();
		 for (SQLUpdateSetItem ui : items) {
			 SQLIdentifierExpr  col=(SQLIdentifierExpr)ui.getColumn();
		// col.getSimpleName()
			 SQLExpr  val=ui.getValue();
			 System.out.println();
		}
		
		
	}

	private static void testInsert(SQLInsertStatement ins,SQL sql) {
		 
		String table=ins.getTableName().getSimpleName();
		String tableS=ins.getTableSource().getName().getSimpleName();
		List<SQLExpr> cols=ins.getColumns();
		SQLIdentifierExpr deletedExpr=new SQLIdentifierExpr("deleted");
		cols.add(deletedExpr);
		Map<String, Object> ps=sql.getNameParameters();
		List<ValuesClause> vcs= ins.getValuesList();
		int i=0;
		String pname=null;
		for (ValuesClause vc : vcs) {
			pname="PARAM_DELETED_"+i;
			vc.addValue(new SQLVariantRefExpr(":"+pname));
			ps.put(pname, "N");
			i++;
		}
		
		 
		
		System.out.println("#R1="+ins.toString());
		sql=new SE(ins.toString(),ps);
		System.out.println("#Q1="+sql.toString());
		
	}
}
